package tpdisenio2009.model.dao;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import tpdisenio2009.model.HibernateUtils;
import tpdisenio2009.model.beans.FantasyTeam;
import tpdisenio2009.model.beans.Score;
import tpdisenio2009.model.beans.SoccerPlayer;
import tpdisenio2009.model.beans.TournamentDate;

/**
 *
 * @author Pablito
 */
public class ScoreDAO extends AbstractDAO<Score> {

    @Override
    public Class<Score> getPersistentClass() {
        return Score.class;
    }

    public Score getScoreByTournamentDate(TournamentDate date, SoccerPlayer player) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria criteria = session.createCriteria( Score.class );
        criteria.add( Restrictions.eq( "tournamentDate", date ) );
        criteria.add( Restrictions.eq( "soccerPlayer", player ) );

        return (Score) criteria.uniqueResult();
    }

    public List<Score> getByFantasyTeamAndDate( FantasyTeam fantasyTeam, TournamentDate date ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( Score.class ).createAlias( "soccerPlayer", "sp" ).createAlias( "sp.fantasyTeamPlayerCollection", "ftp" );
        mainCriteria.add( Restrictions.eq( "ftp.fantasyTeam", fantasyTeam ) );
        mainCriteria.add( Restrictions.eq( "tournamentDate", date) );

        return mainCriteria.list();
    }

}
